// formPackage/pages/invite/inviteManagerList/inviteManagerList.js
import { queryCompanyEnterList, payRefundAction } from "../../../../service/platform_service"
import { queryTempCompanyList, companyTransNewCompany } from "../../../../service/manager_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    exStatus: 0,
    examineStatus: 1,
    switchArr: [
      {
        title: "待审核",
        key: 1,
      },
      {
        title: "审核成功",
        key: 2
      },
      {
        title: "审核失败",
        key: 3
      },
    ],
    inviteList: [

    ],
    page: 0,
    pageSize: 500,
    searchWords: "",
    showTransPop: false,
    transCompanyList: [

    ],
    searchWords: "",
    oldCompanyNumber: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryCompanyEnterList();
  },
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      exStatus: index,
      examineStatus: item.key
    })
    this.queryCompanyEnterList();
  },
  //获取我自己的招募列表:
  queryCompanyEnterList() {
    wx.showLoading();
    queryCompanyEnterList(this.data.examineStatus, this.data.page, this.data.pageSize, this.data.searchWords).then(res => {
      wx.hideLoading();
      console.log("JJLog:获取到的公司入驻列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          inviteList: res.data.list
        })
      }
    }).catch(err => {
      console.error("JJLog:queryCompanyEnterList", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },

  //查询临时公司列表:
  queryTempCompanyList() {
    wx.showLoading();
    queryTempCompanyList(this.data.searchWords, 0, 10).then(res => {
      wx.hideLoading();
      console.log("JJog:查询到的临时公司列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          transCompanyList: res.data || []
        })

      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:queryTempCompanyList", err);
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },


  pushToDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/formPackage/pages/checkOrManger/companyCheckDetail/companyCheckDetail?id=' + id,
    })
  },

  //选择临时公司:
  selectTCompanyAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let isChoose = item.isChoose;
    let arr = this.data.transCompanyList;
    arr.forEach((i) => {
      i.isChoose = false;
    })
    this.setData({
      transCompanyList: arr,
    })
    let item1 = 'transCompanyList[' + index + '].isChoose';
    this.setData({
      [item1]: !isChoose,
    })

    //确定选中的情况
    if (!isChoose) {
      wx.showModal({
        title: '提示',
        content: '公司迁移后，不可恢复，确认操作?',
        confirmColor: "#FF0000",
        success: res => {
          if (res.confirm) {
            this.companyTransNewCompany(this.data.oldCompanyNumber, item.companyNumber);
          }
        }
      })

    }


  },

  companyTransNewCompany(oldNumber, newNumber) {
    wx.showLoading();
    companyTransNewCompany(oldNumber, newNumber).then(res => {
      wx.hideLoading();
      console.log("JJog:迁移操作:" + JSON.stringify(res));
      if (res.code == 200) {
        this.queryCompanyEnterList();
        this.setData({
          showTransPop: false,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:companyTransNewCompany", err);
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })
  },
  searchAction(e) {
    this.setData({
      searchWords: e.detail.searchWord,
    })
    this.queryTempCompanyList();

  },

  //退款操作:
  refundAction(e) {
    let index = e.currentTarget.dataset.index;
    let orderNumber = e.currentTarget.dataset.number;
    console.log("JJLog:orderNumber=" + orderNumber);
    wx.showModal({
      title: '提示',
      content: '确认对当前申请人退款?',
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.payRefundAction(orderNumber, index);
        }
      }
    })

  },

  //退款操作
  payRefundAction(orderNumber, index) {
    wx.showLoading();
    payRefundAction(orderNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:退款结果:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '退款成功',
          icon: "success",
        })
        let arr = this.data.inviteList;
        arr.splice(index, 1);
        this.setData({
          inviteList: arr,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "退款失败",
        icon: "none"
      })
      console.error("JJLog:payRefundAction", err);
    })
  },


  //迁移公司:
  transferAction(e) {
    this.queryTempCompanyList();
    let number = e.currentTarget.dataset.number;
    this.setData({
      showTransPop: true,
      oldCompanyNumber: number
    })
  },

  clickCenterPopupOverlay() {
    this.setData({
      showTransPop: false,
    })
  }







})